import Vue from 'vue'
import Router from 'vue-router'

// 引入多个模块的规则
import approvalsRouter from './module/approvals'
import departmentsRouter from './module/departments'
import employeesRouter from './module/employees'
import permissionRouter from './module/permission'
import attendancesRouter from './module/attendances'
import salarysRouter from './module/salarys'
import settingRouter from './module/setting'
import socialRouter from './module/social'
import user from './module/user'
Vue.use(Router)

/* Layout */
import Layout from '@/layout'


export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }, {
      path: '/detail/:id',
      hidden: true,   // 在左侧菜单中隐藏
      component: () => import('@/views/employees/components/detail.vue'),
      meta: {
        title: '员工详情' // 标记当前路由规则的中文名称 后续在做左侧菜单时 使用
      }

    }, {
      path: '/print/:id',
      hidden: true,   // 在左侧菜单中隐藏
      component: () => import('@/views/employees/components/print.vue'),
      meta: {
        title: '员工打印' // 标记当前路由规则的中文名称 后续在做左侧菜单时 使用
      }
    }]
  },
  {
    path: '/import',
    component: Layout,
    hidden: true,   // 在左侧菜单中隐藏
    children: [{
      path: '',
      component: () => import('@/views/import'),
    }]
  },
  // 大家都可以访问的
  user

  // 404 page must be placed at the end !!! 没有的页面都去404
  // { path: '*', redirect: '/404', hidden: true }
]

// 动态路由
export const asyncRoutes = [
  approvalsRouter,
  departmentsRouter,
  employeesRouter,
  permissionRouter,
  attendancesRouter,
  salarysRouter,
  settingRouter,
  socialRouter
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),  // 管理滚动行为 如果出现滚动 切换就让 让页面回到顶部
  routes: [...constantRoutes, ...asyncRoutes]  // 临时合并所有的路由
  // routes: [...constantRoutes]
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
// 重置路由
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
